package com.erp.module.crm.xXCrm

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.module.crm.xXCrm.jiaoLiu.JiaoLiuKActivity
import com.erp.activityKView.CrmFKView
import com.erp.myAdapter.JiaoliuAdapter
import com.erp.bean.ClientJiaoliuBean
import com.erp.Interface.OnUserDataInterface
import com.erp.myFragment.BaseKFragment
import com.erp.R
import com.erp.util.Httputil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.fragment_clientseight.*
import org.json.JSONObject



/**
 * Created by Administrator on 2016/12/31/031.
 * 作者：刘洋
 * 交流记录
 */
@SuppressLint("ValidFragment")
class FragmentClientseight : BaseKFragment, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, View.OnClickListener {
    lateinit var mlistjiaoliubean: List<ClientJiaoliuBean> ;
    lateinit var KView : CrmFKView;
    constructor(){
    }
    constructor(KView: CrmFKView,mlistjiaoliubean: List<ClientJiaoliuBean>){
        this.mlistjiaoliubean=mlistjiaoliubean
        this.KView = KView
    }
    private val f = "FragmentClientseight"
    lateinit var adapter: JiaoliuAdapter
    lateinit var updatedialog: AlertDialog

    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_clientseight
    }
    override fun initData() {
        adapter = JiaoliuAdapter(mlistjiaoliubean, context!!)
        LvJiaoliu.adapter = adapter
        if(mlistjiaoliubean.isEmpty()){
            TvZanwu.visibility=View.VISIBLE
        }else{
            TvZanwu.visibility=View.GONE
        }
        LvJiaoliu.onItemClickListener = this
        LvJiaoliu.onItemLongClickListener = this
        TvZanwu.setOnClickListener(this)
    }

    fun notifydata(mlistjiaoliubean: List<ClientJiaoliuBean>) {
        this.mlistjiaoliubean=mlistjiaoliubean
        if(!isFirstLoad) {
            if(mlistjiaoliubean.isEmpty()){
                TvZanwu.visibility=View.VISIBLE
            }else{
                TvZanwu.visibility=View.GONE
            }
            adapter.Allnotifi(this.mlistjiaoliubean)
        }
    }
    override fun onItemLongClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long): Boolean {

        if(position<mlistjiaoliubean.size){
            var c = context;
            if(c != null){
                val dialog = AlertDialog.Builder(c)
                dialog.setTitle("提示")
                dialog.setMessage("确定要删除这条交流记录问题?")
                dialog.setPositiveButton("确定") { dialog, which ->
                    val jo=xiugai(position)
                    updatedialog = Myutil.showupdatedialog(activity, "正在删除", false)
                    Httputil.cleanclient(activity, ClientKActivity.getid(),jo,object : OnUserDataInterface {
                        override fun onUserData(data: String?) {
                            Log.d("TAAG","data="+data.toString())
                            updatedialog.dismiss()
                            if(data==null){
                                return
                            }
                            KView.updata(data)
                        }

                        override fun onNot(throwable: String?) {
                            updatedialog.dismiss()
                            Log.d("TAAG","throwable="+throwable)
                        }

                    })
                }
                dialog.setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                dialog.show()
            }

        }
        return true
    }
    private fun xiugai(i : Int): JSONObject {
        var jo = JSONObject()
        var db = mlistjiaoliubean[i]
        try {
            val jiaoliu = JSONObject()
            jiaoliu.put("id", db.id)
            jiaoliu.put("relative_people",db.relative_people)
            jiaoliu.put("communication_time",Myutil.getdata(db.communication_time))
            jiaoliu.put("content",db.content)
            jiaoliu.put("appointment_communication_time",Myutil.getdata(db.appointment_communication))
            jiaoliu.put("isdelete",true)
            jo.put("operations-add", jiaoliu)
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("TAAAG","--------------"+e.toString())
        }
        return jo
    }
    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        var i = Intent(activity, JiaoLiuKActivity:: class.java)
        if(position<mlistjiaoliubean.size){
            bean =mlistjiaoliubean[position]
            i.putExtra("isadd",false)
        }else{
            bean =ClientJiaoliuBean()
            i.putExtra("isadd",false)
        }
        this@FragmentClientseight.startActivityForResult(i,50)
    }
    override fun onClick(v: View?) {
        when(v){
            TvZanwu -> {
                var i = Intent(activity, JiaoLiuKActivity:: class.java)
                bean =ClientJiaoliuBean()
                i.putExtra("isadd",false)
                this@FragmentClientseight.startActivityForResult(i,50)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null){
            return
        }
        Log.d("TAAG","------------------------------------")
        if(resultCode==Activity.RESULT_OK){
            when(requestCode){
                50 -> {
                    val d=data.getStringExtra("data")
                    KView.updata(d)
                }
            }
        }
    }
    companion object {
        var bean : ClientJiaoliuBean = ClientJiaoliuBean()
    }
}
